﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.Supervise.Model.Enum;
using System.Data;

namespace HZ.Supervise.Web.Message
{
    public partial class WarningStart : BasePage
    {
        IBLL.IBLL_MessageTypeDetail ibllMessage = new BLL.BLL_MessageTypeDetail();

        public WarningStart()
        {
            this.ModuleNo = Module.预警控制.ToString();
            this.PageOperateType = OperateType.已禁用的预警.ToString();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindList();
            }
        }
        /// <summary>
        /// 数据绑定
        /// </summary>
        void BindList()
        {
            
            int t = 0;
            Model.MessageTypeDetail messageTypeDetail = new HZ.Supervise.Model.MessageTypeDetail();
            //messageTypeDetail.ProductName = txtProductName.Text.Trim();
            //messageTypeDetail.CompanyName = txtCompany.Text.Trim();
            //messageTypeDetail.AreaName = txtArea.Text.Trim();
            //messageTypeDetail.HospitalName = txtHospital.Text.Trim();
            //messageTypeDetail.SortName = txtDrugSort.Text.Trim();
            messageTypeDetail.UserId = CurrentAccount.UserID.ToString();
            try
            {
                if (!string.IsNullOrEmpty(txtCount.Text.Trim()))
                {
                    string count = DAL.DAL_Helper.Replace(txtCount.Text.Trim());
                    if (count != "")
                    {
                        messageTypeDetail.CountRight = Convert.ToInt64(count);
                    }
                }
                if (!string.IsNullOrEmpty(txtAmount.Text.Trim()))
                {
                    string amount = DAL.DAL_Helper.Replace(txtAmount.Text.Trim());
                    if (amount != "")
                    {
                        messageTypeDetail.AmountRight = decimal.Parse(txtAmount.Text.Trim());
                    }
                }
            }
            catch (Exception)
            {
                HZ.Web.WebUtil.Alert("只能输入数字!");
                
            }
            
            messageTypeDetail.Level = ddlLevel.SelectedItem.ToString();           
            messageTypeDetail.Is_Del = false;//未删除的
            messageTypeDetail.Is_Work = false ;//未启用的          

            DataTable dt = new DataTable();
            dt = ibllMessage.GetMessageTypeDetailByType(messageTypeDetail, "", AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out t);
            //HZ.Supervise.Web.Function.AddColumn(dt, "SortSource");
            gvwWarningStart.DataSource = dt;
            AspNetPager1.RecordCount = t;
            gvwWarningStart.DataBind();
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindList();
        }
        /// <summary>
        /// 行数据绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwWarningStart_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            for (int i = 0; i < gvwWarningStart.Rows.Count; i++)
            {
                string areaName = ((Label)(gvwWarningStart.Rows[i].Cells[2].FindControl("lblAreaName"))).Text.ToString();
                ((Label)(gvwWarningStart.Rows[i].Cells[2].FindControl("lblAreaName"))).Text = SubStr(areaName, 12);

                string hospitalName = ((Label)(gvwWarningStart.Rows[i].Cells[1].FindControl("lblHospitalName"))).Text.ToString();
                if (hospitalName != "")
                {
                    ((Label)(gvwWarningStart.Rows[i].Cells[1].FindControl("lblHospitalName"))).Text = SubStr(hospitalName, 12);
                }
                else
                {
                    ((Label)(gvwWarningStart.Rows[i].Cells[1].FindControl("lblHospitalName"))).Text = "所有医院";
                }

                string productName = ((Label)(gvwWarningStart.Rows[i].Cells[4].FindControl("lblProductName"))).Text.ToString();
                if (productName != "")
                {
                    ((Label)(gvwWarningStart.Rows[i].Cells[4].FindControl("lblProductName"))).Text = SubStr(productName, 12);
                }
                else
                {
                    ((Label)(gvwWarningStart.Rows[i].Cells[4].FindControl("lblProductName"))).Text = "所有药品";
                }
                string SortName = ((Label)(gvwWarningStart.Rows[i].Cells[5].FindControl("lblSortName"))).Text.ToString();
                if (SortName != "---请选择---")
                {
                    ((Label)(gvwWarningStart.Rows[i].Cells[5].FindControl("lblSortName"))).Text = SubStr(SortName, 12);
                }
                else
                {
                    ((Label)(gvwWarningStart.Rows[i].Cells[5].FindControl("lblSortName"))).Text = "所有分类";
                }

            }
        }
        /// <summary>
        /// 字符串截取
        /// </summary>
        /// <param name="sString"></param>
        /// <param name="nLeng"></param>
        /// <returns></returns>
        string SubStr(string sString, int nLeng)
        {
            if (sString.Length <= nLeng)
            {
                return sString;
            }
            string sNewStr = sString.Substring(0, nLeng);
            sNewStr = sNewStr + "...";
            return sNewStr;
        }
        /// <summary>
        /// 删除预警
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDelte_Click(object sender, EventArgs e)
        {
            string chkedids = Request.Form["chk"];
            int flag = 1;
            foreach (var item in chkedids.Split(','))
            {
                flag= ibllMessage.DeleteMessageTypeDetail(item);
            }
            if (flag==1)
            {
                HZ.Web.WebUtil.Alert("删除成功！");
            }
            else
            {
                HZ.Web.WebUtil.Alert("删除失败！");
            }
            BindList();
        }
        /// <summary>
        /// 启用预警
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnStart_Click(object sender, EventArgs e)
        {
            string chkedids = Request.Form["chk"];
            int flag = 1;
            foreach (var item in chkedids.Split(','))
            {
                flag = ibllMessage.WarningSwitch(item,"1");
            }
            if (flag == 1)
            {
                HZ.Web.WebUtil.Alert("启用成功！");
            }
            else
            {
                HZ.Web.WebUtil.Alert("启用失败！");
            }
            BindList();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtAmount.Text) || !string.IsNullOrEmpty(txtCount.Text))
            {
                if (txtAmount.Text.Equals("0") || txtCount.Text.Equals("0"))
                    HZ.Web.WebUtil.Alert("预警金额和预警数量都不能为0");
                else
                {
                    AspNetPager1.CurrentPageIndex = 1;
                    //BindList();
                }
            }
            else
            {
                AspNetPager1.CurrentPageIndex = 1;
                //BindList();
            }
        }

    }
}
